// pages/user/user.js
var app = getApp();
console.log(app.globalData)
Page({

  /**
   * 页面的初始数据
   */
  data: {
  
  },
  go_url:function(e){
    var that = this;
    console.log(e.currentTarget.dataset.url);
    if (e.currentTarget.dataset.url == "../luck_draw/luck_draw"){
      wx.navigateTo({
        url: "../luck_draw/luck_draw?id=" + that.data.huiyuanxinxi.lottery,
      })
    }else{
      wx.navigateTo({
        url: e.currentTarget.dataset.url,
      })
    }
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    if(app.globalData.userInfo == ""){
      wx.getSetting({
        success: function (res) {
          if (res.authSetting['scope.userInfo']) {
            wx.getUserInfo({
              success: function (res) {
                //从数据库获取用户信息
                console.log(res)
                app.globalData.userInfo = res.userInfo;
                that.setData({
                  user: res.userInfo
                })

              }
            });
          }else{
            wx.navigateTo({
              url: '../login/login',
            })
          }
        }
      })
    }
    var that= this;
    wx.showLoading({
      title: '加载中',
    })
    if(app.globalData.opid == ""){
      wx.hideLoading()
      wx.navigateTo({
        url: '../login/login',
      })
    }
    console.log('用户信息')
    console.log(app.globalData.userInfo)
    that.setData({
      user: app.globalData.userInfo
    })
    // 抽奖次数+累计奖金
    wx.request({
      url: app.globalData.url +'jingcai.php?Action=huiyuanxinxi',
      data:{
        openid:app.globalData.opid
      },
      success:function(res){
        console.log(res)
        wx.hideLoading()
        that.setData({
          huiyuanxinxi: res.data
        })
        
      }
    })
    // 支付总金额
    wx.request({
      url:app.globalData.url +'ny_flow_pay.php?action=saomajiner',
      data: {
        openid: app.globalData.opid
      },
      success:function(res){
        console.log(res)
        that.setData({
          zongjiner:res.data
        })
      }
    })

    var myCanvasWidth;
    var myCanvasHeight;

    wx.getSystemInfo({
      success: function (res) {
        console.log(res.windowWidth)
        console.log(res.windowHeight)
        myCanvasWidth = res.windowWidth;
        myCanvasHeight = res.windowHeight;
      },
    })
    that.setData({
      canvasWidth: myCanvasWidth,
      canvasHeight: myCanvasHeight
    })

  },

  shengcheng: function () {
    var that = this;
    
    wx.showLoading({
      title: '海报生成中...',
    })
    that.setData({
      zhe: true,
      fenxiang: true

    })
   
    wx.request({
      url: app.globalData.url + 'ziyu.php?Action=scerweima',
      data: {
        openid:app.globalData.opid
      },
      success: function (res) {
        console.log(res)
        
        wx.downloadFile({
          url: res.data.erweima,
          success: function (res) {
            console.log(res.tempFilePath);
            var width = that.data.canvasWidth;
            var rem = width / 750;
            console.log(width)
            console.log(rem)
            var context = wx.createCanvasContext('myCanvas');
            context.setFillStyle("#fff");
            context.fillRect(0, 0, 700 * rem, 1250 * rem);

           

            var path = "./img/share1.png";
            // 将模板图片绘制到canvas,在开发工具中drawImage()函数有问题，不显示图片
            // 不知道是什么原因，手机环境能正常显示t
            context.drawImage(path, 0, 0, 700 * rem, 1250 * rem);
            console.log(that.data.user.nickName)
            context.setFontSize(48 * rem)
            context.setFillStyle("#fff");
            context.setTextAlign('center')
            context.fillText(that.data.user.nickName, 350 * rem, 330 * rem);



            // var name1 = "我已投票，支持" + that.data.detail.option_name;
            // console.log(name1)
            context.setFontSize(32 * rem)
            context.setFillStyle("#535353");
            context.setTextAlign('center')
            context.fillText("微信识别二维码，跟我一起来集赞！", 350 * rem, 905 * rem);


            // context.setFontSize(32 * rem)
            // context.setFillStyle("#333333");
            // context.setTextAlign('left')
            // context.fillText("参赛宣言：", 40 * rem, 670 * rem);


            // var name3 = that.data.detail.option_content;

            // context.setFontSize(28 * rem)
            // context.setFillStyle("#666");
            // context.setTextAlign('left')
            // // context.fillText(name3, 40*rem, 720*rem);
            // that.drawText(context, name3, 40 * rem, 720 * rem, 148, (480 * rem))



            // context.setFontSize(36 * rem)
            // context.setFillStyle("#161616");
            // context.setTextAlign('left')
            // context.fillText("微信长按识别二维码", 360 * rem, 900 * rem);
            // context.fillText("为我助力吧", 360 * rem, 960 * rem);

            // context.setFillStyle("#fff");
            // context.setFontSize(40 * rem)
            // // context.font = 'normal bold sans-serif';
            // context.setTextAlign('center')
            // context.fillText(name, 350 * rem, 470 * rem);


            var path1 = res.tempFilePath;
            console.log(path1)
            context.drawImage(path1, 215 * rem, 550 * rem, 270 * rem, 270 * rem);



            wx.downloadFile({
              url: that.data.user.avatarUrl,
              success: res => {

                var avatarurl_width = 160*rem;    //绘制的头像宽度
                var avatarurl_heigth = 160 * rem;   //绘制的头像高度
                var avatarurl_x = 270 * rem;   //绘制的头像在画布上的位置
                var avatarurl_y = 100 * rem;   //绘制的头像在画布上的位置
                context.save();
                context.beginPath(); //开始绘制
                context.setFillStyle("#fff");
                context.arc(avatarurl_width / 2 + avatarurl_x, avatarurl_heigth / 2 + avatarurl_y,85*rem,0, Math.PI * 2, false);
                
                context.clip();
                context.fill()
                context.restore();

                context.save();
                context.beginPath();
                //先画个圆   前两个参数确定了圆心 （x,y） 坐标  第三个参数是圆的半径  四参数是绘图方向  默认是false，即顺时针
                context.arc(avatarurl_width / 2 + avatarurl_x, avatarurl_heigth / 2 + avatarurl_y, avatarurl_width / 2, 0, Math.PI * 2, false);
                
                context.clip();
                context.drawImage(res.tempFilePath, avatarurl_x, avatarurl_y, avatarurl_width, avatarurl_heigth); 
               
                context.restore(); //恢复之前保存的绘图上下文 恢复之前保存的绘图上下午即状态 还可以继续绘制
                context.draw();
              }, fail: res => {
                console.log(res);
              }
            })
           
           
            
           
            setTimeout(function () {
              wx.canvasToTempFilePath({
                x: 0,
                y: 0,
                width: 700 * rem,
                height: 1250 * rem,
                destWidth: 700 * rem * 2,
                destHeight: 1250 * rem * 2,
                canvasId: 'myCanvas',
                success: function (res) {
                  console.log(res.tempFilePath)

                  var arr = [];
                  arr.push(res.tempFilePath)
                  wx.previewImage({
                    current: "", // 当前显示图片的http链接
                    urls: arr, // 需要预览的图片http链接列表
                    success: function (res) {
                      wx.hideLoading()
                      wx.request({
                        url: app.globalData.url + 'voting.php?Action=ffshare',
                        data: {
                          openid: app.globalData.opid,
                          S_types: 3,
                          S_productid: that.data.id
                        },
                        success: function (res) {
                          console.log(res)
                        }
                      })
                      that.setData({
                        zhe: false,
                        fenxiang: false

                      })
                    }
                  })

                },
                fail: function (res) {
                  console.log(res)
                  wx.hideLoading()
                  wx.showToast({
                    title: '海报生成失败，请重试',
                    icon: "none"
                  })
                }
              })
            }, 1000);

          }, fail: function (fres) {

          }
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // 抽奖次数+累计奖金
    var that = this;
    console.log(app.globalData)
    that.setData({
      user: app.globalData.userInfo
    })
    wx.request({
      url: app.globalData.url + 'jingcai.php?Action=huiyuanxinxi',
      data: {
        openid: app.globalData.opid
      },
      success: function (res) {
        console.log(res)
        wx.hideLoading()
        that.setData({
          huiyuanxinxi: res.data
        })

      }
    })
    // 支付总金额
    wx.request({
      url: app.globalData.url + 'ny_flow_pay.php?action=saomajiner',
      data: {
        openid: app.globalData.opid
      },
      success: function (res) {
        console.log(res)
        that.setData({
          zongjiner: res.data
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})